import { Component, ViewChild, OnInit, EventEmitter, Input, Output, SimpleChanges } from '@angular/core';
import { Page } from '../../shared/components/pagination/pagination.model';
import { PaginationComponent } from '../../shared/components/pagination/pagination.component';
import { Http } from '@angular/http';
//Models
import { AuthModel } from '../../models/auth.model';
//Services
import { ListingRiskService } from '../listingRisk.service';
import { RiskBaseComponent } from '../../shared/components/riskBase.component';
import { UtilsService } from '../../shared/services/utils.service';

@Component({
    selector: 'list-risk-rating',
    templateUrl: './companyList.component.html',
    styleUrls: ['./companyList.component.css'],
    providers: [UtilsService]
})

export class CompaniesListComponent extends RiskBaseComponent implements OnInit {
    public companies: any[];
    public companyCount: number;

    private showLegend: boolean = true;
    public orderByCol: string = "CompanyName";
    public orderByAsc: boolean = true;
    public dropdownSettings: any;
    
    public riskClusters: any[];
    public compFilters: any = {
        companyName: '',        
        subPeerGroup: '',
        assessments: []
    };

    @ViewChild('pagination') pagination: PaginationComponent;
    @Output() onLoad: EventEmitter<void> = new EventEmitter<void>();
    @Input('url') containerUrl: string;

    constructor(
        private riskService: ListingRiskService,
        private http: Http) {
        super(http);
     }

    ngOnChanges(changes: SimpleChanges) {
        this.getCompanies().then(()=>{
            this.onLoad.emit();
        });
    }

    ngOnInit() {
        this.getCompanyClusters().then(clusters =>{
            this.riskClusters = clusters;
        });  

        this.dropdownSettings = {
            enableSearch: false,
            buttonClasses: 'multi-select-dropdown-button btn btn-default btn-secondary',
            checkedStyle: 'glyphicon',
            dynamicTitleMaxItems: 3,
            showCheckAll: true,
            showUncheckAll: true,
            displayAllSelectedText: true
        };
    }

    onDropdownClosed(item) {
        this.getCompanies();
    }

    public getCompanyUrlFriendlyName(company: any): string {
        return UtilsService.getCompanyUrlFriendlyName(company);
    }

    public setOrderByCol(col: string): void {
        if (this.orderByCol === col) {
            this.orderByAsc = !this.orderByAsc;
        }
        else {
            this.orderByCol = col;
            this.orderByAsc = true;
        }
        this.getCompanies();
    }    

    public searchButtonClick(): void {
        this.pagination.goTo(1);
    }

    public pageChanged(page: Page) {
        this.getBulkCompanies(page.skip, page.take);
    }

    private getCompanies(): Promise<void> {
        return this.getBulkCompanies(this.pagination.skipCount(), this.pagination.takeCount());
    }

    private getBulkCompanies(skip: number, take: number): Promise<void> {      
        let promise = new Promise<void>((resolve, reject) => {
            var promiseGet: Promise<any[]> = this.riskService.getCompanies(null, this.compFilters, skip, take, this.orderByCol, this.orderByAsc);
            var promiseCount: Promise<number> = this.riskService.getCompaniesCount(null, this.compFilters);
            
            Promise.all([promiseGet, promiseCount]).then((data) => {
                this.companies = data[0] as any[];
                this.companyCount = (data[1] as any).companyCount;
                resolve();
            });
        });

        return promise;
    }
}